<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('business.activation.code.export.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="100px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('business.activation.code.export.total')" prop="total">
            <el-input-number v-model="dataForm.total" :min="1">
            </el-input-number>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>

            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="activation-code-export">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/activation/code')

  const dataForm = reactive({
    total: 1,
  })

  const dataRule = reactive({
    total: [{
      required: true,
      message: i18n('business.activation.code.export.rules.total.require'),
      trigger: 'blur'
    }]
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/export`),
          method: 'post',
          data: $http.adornData(dataForm),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            window.open(data.data)
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }
</script>
